/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techshop.dao.impl;

import com.techshop.dao.ReceiptNoteDetailDao;
import com.techshop.model.ReceiptNoteDetail;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 * @author Hai-PC
 */
public class ReceiptNoteDetailDaoImpl extends HibernateDaoSupport implements ReceiptNoteDetailDao
{
    @Override
    public ReceiptNoteDetail getById(int receiptNoteDetailId)
    {
        try
        {
            return (ReceiptNoteDetail) getSession().get(ReceiptNoteDetail.class, receiptNoteDetailId);
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public boolean insert(ReceiptNoteDetail receiptNoteDetail)
    {
        try
        {
            getHibernateTemplate().save(receiptNoteDetail);
            return true;
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }
    
    @Override
    public boolean update(ReceiptNoteDetail receiptNoteDetail)
    {
        try
        {
            if (getById(receiptNoteDetail.getId()) == null)
            {
                return false;
            }
            else
            {
                getHibernateTemplate().update(getHibernateTemplate().merge(receiptNoteDetail));
                return true;
            }
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public boolean delete(ReceiptNoteDetail receiptNoteDetail)
    {
        try
        {
            if (getById(receiptNoteDetail.getId()) == null)
            {
                return false;
            }
            else
            {
                getHibernateTemplate().delete(getHibernateTemplate().merge(receiptNoteDetail));
                return true;
            }
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }
}